package com.execjar.tcptools;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 向业务前置发送请求
 *
 * @author dehuisun
 *
 */
public class TcpUtils {

	private static int timeOut = 200;
	private static final Logger log = LoggerFactory.getLogger(TcpUtils.class);

	/**
	 * tcpPost
	 *
	 * @param param 参数
	 * @throws IOException
	 */
	public static String post(String ip, int port, String data) throws IOException {
		Socket socket = null;
		InputStream is = null;
		byte message[] = null;
		DataOutputStream dos = null;
		String value = "";
		try {
			String tradeMessage = data;
			socket = new Socket(ip, port);
			socket.setSoTimeout(timeOut);
			byte[] msg = tradeMessage.getBytes("UTF-8");
			dos = new DataOutputStream(socket.getOutputStream());
			dos.write(msg);
			dos.flush();
			message = new byte[1024];
			is = socket.getInputStream();
			is.read(message);// 接收服务器的响应信息
			value = new String(message);
			log.info("返回的数据：" + value);
		} catch (Exception e) {
			log.error("error equ_ip:{}|{}", ip, e.getMessage());
		} finally {// 关闭连接和I/O,释放资源
			try {
				if (is != null) {
					is.close();
				}
				// 关闭连接和I/O,释放资源
				if (dos != null) {
					dos.close();
				}
			} catch (Exception e) {
			} finally {
				socket.close();
			}
		}
		return value;
	}

	/**
	 * tcpPost
	 *
	 * @param param 参数
	 * @throws IOException
	 */
	public static void postwithoutresponse(String ip, int port, String data) throws IOException {
		Socket socket = null;
		DataOutputStream dos = null;
		try {
			String tradeMessage = data;
			socket = new Socket(ip, port);
			socket.setSoTimeout(timeOut);
			byte[] msg = tradeMessage.getBytes("UTF-8");
			dos = new DataOutputStream(socket.getOutputStream());
			dos.write(msg);
			dos.flush();

		} catch (Exception e) {
			log.error("error equ_ip:{}|{}", ip, e.getMessage());
		} finally {// 关闭连接和I/O,释放资源
			try {
				// 关闭连接和I/O,释放资源
				if (dos != null) {
					dos.close();
				}
			} catch (Exception e) {
			} finally {
				socket.close();
			}
		}
	}

	/**
	 * tcpPost返回字节流
	 *
	 * @param param 参数
	 * @throws IOException
	 */
	public static byte[] postReByte(String ip, int port, String data) throws IOException {
		Socket socket = null;
		InputStream is = null;
		byte message[] = null;
		DataOutputStream dos = null;
		try {
			String tradeMessage = data;
			socket = new Socket(ip, port);
			socket.setSoTimeout(timeOut);
			byte[] msg = tradeMessage.getBytes("UTF-8");
			dos = new DataOutputStream(socket.getOutputStream());
			dos.write(msg);
			dos.flush();
			message = new byte[1024];
			is = socket.getInputStream();
			is.read(message);// 接收服务器的响应信息
			// log.info("返回的数据：");
			// value = new String(message);
		} catch (Exception e) {
			log.error("error equ_ip:{}|{}", ip, e.getMessage());
		} finally {// 关闭连接和I/O,释放资源
			try {
				if (is != null) {
					is.close();
				}
				// 关闭连接和I/O,释放资源
				if (dos != null) {
					dos.close();
				}
			} catch (Exception e) {
			} finally {
				socket.close();
			}
		}
		return message;
	}

	/**
	 * tcpPost 按照发送字节
	 *
	 * @param byte[] 参数
	 * @throws IOException
	 */
	@SuppressWarnings("finally")
	public static String postBytes(String ip, int port, byte[] msg) throws Exception {
		Socket socket = null;
		InputStream is = null;
		byte message[] = null;
		DataOutputStream dos = null;
		String value = "";
		try {
			socket = new Socket(ip, port);
			socket.setSoTimeout(timeOut);
			dos = new DataOutputStream(socket.getOutputStream());
			dos.write(msg);
			dos.flush();
			message = new byte[1024];
			is = socket.getInputStream();
			is.read(message);// 接收服务器的响应信息
			value = new String(message);
		} catch (Exception e) {
			log.error("error equ_ip:{}|{}", ip, e.getMessage());
		} finally {
			// 关闭连接和I/O,释放资源
			try {
				if (is != null) {
					is.close();
				}
				// 关闭连接和I/O,释放资源
				if (dos != null) {
					dos.close();
				}
			} catch (Exception e) {
			} finally {
				socket.close();
			}
			return value;
		}

	}

	/**
	 * tcpPost返回指定字节的16进制字符串
	 *
	 * @param param 参数
	 * @throws IOException
	 */
	public static String postRe16Self(String ip, int port, byte[] data, int length) {
		Socket socket = null;
		InputStream is = null;
		byte message[] = null;
		DataOutputStream dos = null;
		String mes = "";
		try {
			socket = new Socket(ip, port);
			socket.setSoTimeout(timeOut);
			dos = new DataOutputStream(socket.getOutputStream());
			dos.write(data);
			dos.flush();
			message = new byte[length];
			is = socket.getInputStream();
			is.read(message);// 接收服务器的响应信息
			mes = HexStrConver.byte2HexStr(message);
			//log.info("返回的数据：" + mes);
		} catch (Exception e) {
			log.error("error equ_ip:{}|{}", ip, e.getMessage());
		} finally {// 关闭连接和I/O,释放资源
			closeIO(is, dos, socket);
		}
		return mes;
	}

	/**
	 * tcpPost返回16进制字符串
	 *
	 * @param param 参数
	 * @throws IOException
	 */
	public static String postRe16(String ip, int port, String data) throws IOException {
		Socket socket = null;
		InputStream is = null;
		byte message[] = null;
		DataOutputStream dos = null;
		String mes = "";
		try {
			String tradeMessage = data;
			socket = new Socket(ip, port);
			socket.setSoTimeout(timeOut);
			byte[] msg = tradeMessage.getBytes("UTF-8");
			dos = new DataOutputStream(socket.getOutputStream());
			dos.write(msg);
			dos.flush();
			message = new byte[1024];
			is = socket.getInputStream();
			is.read(message);// 接收服务器的响应信息
			mes = HexStrConver.byte2HexStr(message);
			log.info("返回的数据：" + mes);
			// value = new String(message);
		} catch (Exception e) {
			log.error("error equ_ip:{}|{}", ip, e.getMessage());
		} finally {// 关闭连接和I/O,释放资源
			try {
				if (is != null) {
					is.close();
				}
				// 关闭连接和I/O,释放资源
				if (dos != null) {
					dos.close();
				}
			} catch (Exception e) {
			} finally {
				socket.close();
			}
		}
		return mes;
	}

	/**
	 * 特殊处理为监控PLC状态 tcpPost16进制字符串
	 *
	 * @param param 参数
	 * @throws IOException
	 */
	public static String post16Re16(String ip, int port, String data) throws IOException {
		Socket socket = null;
		InputStream is = null;
		byte message[] = null;
		DataOutputStream dos = null;
		String mes = "";
		try {
			socket = new Socket(ip, port);
			socket.setSoTimeout(timeOut);
			byte[] msg = HexStrConver.hexStringToBytes(data);
			// byte[] msg = tradeMessage.getBytes("UTF-8");
			dos = new DataOutputStream(socket.getOutputStream());
			dos.write(msg);
			dos.flush();
			message = new byte[1];
			// message=HexStrConver.toByteArray(data);
			is = socket.getInputStream();
			is.read(message);// 接收服务器的响应信息
			mes = HexStrConver.byte2HexStr(message);
			log.info("返回的数据：" + mes);
			// value = new String(message);
		} catch (Exception e) {
			log.error("error equ_ip:{}|{}", ip, e.getMessage());
		} finally {// 关闭连接和I/O,释放资源
			try {
				if (is != null) {
					is.close();
				}
				// 关闭连接和I/O,释放资源
				if (dos != null) {
					dos.close();
				}
			} catch (Exception e) {
			} finally {
				socket.close();
			}
		}
		return mes;
	}

	/**
	 * 阿日哈沙特栏杆 tcpPost16进制字符串
	 *
	 * @param param 参数
	 * @throws IOException
	 */
	public static String post16Re16Arhst(String ip, int port, String data) throws IOException {
		Socket socket = null;
		InputStream is = null;
		byte message[] = null;
		DataOutputStream dos = null;
		String mes = "";
		try {
			socket = new Socket(ip, port);
			socket.setSoTimeout(timeOut);
			byte[] msg = HexStrConver.hexStringToBytes(data);
			// byte[] msg = tradeMessage.getBytes("UTF-8");
			dos = new DataOutputStream(socket.getOutputStream());
			dos.write(msg);
			dos.flush();
			message = new byte[7];
			// message=HexStrConver.toByteArray(data);
			is = socket.getInputStream();
			is.read(message);// 接收服务器的响应信息
			mes = HexStrConver.byte2HexStr(message);
			log.info("返回的数据：" + mes);
			// value = new String(message);
		} catch (Exception e) {
			log.error("error equ_ip:{}|{}", ip, e.getMessage());
		} finally {// 关闭连接和I/O,释放资源
			try {
				if (is != null) {
					is.close();
				}
				// 关闭连接和I/O,释放资源
				if (dos != null) {
					dos.close();
				}
			} catch (Exception e) {
			} finally {
				socket.close();
			}
		}
		return mes;
	}

	/**
	 * tcpPost 测试请求
	 *
	 * @param byte[] 参数
	 * @throws IOException
	 */
	public static boolean postBytesReBoolen(String ip, int port, byte[] msg) throws Exception {
		Socket socket = null;
		DataOutputStream dos = null;
		try {
			socket = new Socket();
			socket.connect(new InetSocketAddress(ip, port), 100);// 设置连接请求超时时间100 毫秒
			socket.setSoTimeout(100);
			dos = new DataOutputStream(socket.getOutputStream());
			dos.write(msg);
			dos.flush();
			return true;
		} catch (Exception e) {
			return false;
		} finally {// 关闭连接和I/O,释放资源
			try {
				// 关闭连接和I/O,释放资源
				if (dos != null) {
					dos.close();
				}
			} catch (Exception e) {
			} finally {
				socket.close();
			}
		}
	}

	private static void closeIO(InputStream is, DataOutputStream dos, Socket socket) {
		if (is != null) {
			try {
				is.close();
			} catch (IOException e) {
			}
		}
		if (dos != null) {
			try {
				dos.close();
			} catch (IOException e) {

			}
		}
		if (socket != null) {
			try {
				socket.close();
			} catch (IOException e) {
			}
		}
	}
}
